<?php
class Model_User extends Zend_Db_Table_Abstract
{
	protected $_name    = 'g1_user';
	protected $_primary = 'id';

	public function getAuthAdapter($login, $password){
		

		$adapter = new Zend_Auth_Adapter_DbTable(
				$this->getAdapter(),
				$this->_name,
				'username',  
				'password'
				
		);
	
		$adapter->setIdentity($login);
		$adapter->setCredential($password);

		return $adapter;
	}
	
	public function getAllUsers($order = 'desc'){
		if($order=='desc'){
			$order='desc';
		}else{
			$order='asc';
		}
		$select = $this->select()
				->order('id '.$order);
		$row = $this->fetchAll($select);
		return $row;
	}
	
	public function getAllActiveUsers($order = 'desc'){
		if($order=='desc'){
			$order='desc';
		}else{
			$order='asc';
		}
		$select = $this->select()
		->where('status=?','active')
		->order('id '.$order);
		$row = $this->fetchAll($select);
		return $row;
	}
	
	public function createNewUser($username,$password){
	
		$data['username'] = $username;
		$data['password'] = $password;

		$this->insert($data);
		$user_id = $this->getAdapter()->lastInsertId();
	
		
	
		return $user_id;
	
	}
	
	public function getUserById($user_id){
		$select = $this->select()
				->where('id=?',$user_id);
		$row = $this->fetchRow($select);
		return $row;
	}
	
	public function getUserByName($user_name){
		$select = $this->select()
		->where('username=?',$user_name);
		$row = $this->fetchRow($select);
		return $row;
	}
	
	public function updateUserPassword($user_id,$password){
		$data = array('password'  => $password);		
		$this->update($data, 'id='.$user_id);
	}
	
	public function isUsernameDispo($username){
		$select = $this->select()
				->from($this, 'count(id) as count')
				->where('username = ?', $username);
		 
		$count = $this->fetchRow($select);
		
		$dispo = ($count->count == 0);
		return $dispo;
	}
	
	public function disactiveUserById($user_id){
		$data = array('status'  => 'disactive');
		$this->update($data, 'id='.$user_id);
	}
	
	public function activeUserById($user_id){
		$data = array('status'  => 'active');
		$this->update($data, 'id='.$user_id);
	}
}